import java.util.List;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ModifyConfig {
	public static void main(String[] args)
	{
		String usage="The usage is: ModifyConfig filename orginalString newString";
		System.out.println(usage);
		if(3==args.length)
		{
			System.out.println("the file name is "+args[0]);
			String filename =args[0];
			System.out.println("the orginal string is "+args[1]);
			String orginalString =args[1];
			System.out.println("the new string is "+args[2]);
			String newString = args[2];
			
			//replace
			try {
            Pattern pattern = Pattern.compile(orginalString, Pattern.CASE_INSENSITIVE); 
            Matcher matcher = pattern.matcher("");
            List<String> lines = Files.readAllLines(Paths.get(filename)); 
            for (int i = 0; i < lines.size(); i++) {
                matcher.reset(lines.get(i));
                if (matcher.find()) { 
                    lines.remove(i);
                    lines.add(i, newString);
                }
            }
 
            Files.write(Paths.get(filename), lines);
        } catch (IOException e) {
            e.printStackTrace();
        }
		}
		
       
	}

}
